package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.MdmHelpActivity;
import cn.com.connext.scrm.domain.master.MdmTemplateConfirm;
import cn.com.connext.scrm.domain.master.PrizeGood;
import cn.com.connext.scrm.service.MdmHelpActivityService;
import cn.com.connext.scrm.service.MdmTemplateConfirmService;
import cn.com.connext.scrm.service.PrizeGoodService;
import cn.com.connext.scrm.service.dto.*;
import cn.com.connext.scrm.web.rest.vm.*;
import com.alibaba.fastjson.JSON;
import com.codahale.metrics.annotation.Timed;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeoutException;

/**助力活动后台配置
 * Created by WangZhan on 2019/01/24.
 */
@RestController
@RequestMapping("/api/mdmhelpactivity")
public class MdmHelpActivityResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(MdmHelpActivityResource.class);

    @Autowired
    private MdmHelpActivityService activityService;
    @Autowired
    private MdmTemplateConfirmService mdmTemplateConfirmService;
    @Autowired
    private PrizeGoodService prizeGoodService;
    public MdmHelpActivityResource() throws TimeoutException {
        super("MdmHelpActivity");
    }

    /**
     * 创建助力活动
     */
    @PostMapping("/add")
    @ApiOperation(value = "创建助力活动", notes = "创建助力活动")
    public JsonResult create(@RequestBody MdmHelpActivityVM req) {
        log.info("创建助力活动，入参req : {}", JSON.toJSONString(req));
        return activityService.save(req);
    }


    /**
     * findAllMdmGood
     */
    @PostMapping("/findAllMdmGood")
    @ApiOperation(value = "查询MDM配置的奖品", notes = "查询MDM配置的奖品")
    public JsonResult findAllMdmGood(@RequestBody DrawActivityVM req) {
        log.info("查询助力活动奖品，入参req : {}", JSON.toJSONString(req));
        return prizeGoodService.findAllMdmGood(req);
    }


    /**
     * 根据id查询详情
     */
    @PostMapping("/findById")
    @ApiOperation(value = "根据id查询详情", notes = "根据id查询详情")
    public JsonResult findMdM(@RequestBody MdmHelpActivityVM req) {
        log.info("根据id查询详情，入参req : {}", JSON.toJSONString(req));
        return activityService.findById(req);
    }


    /**
     * 更新助力活动
     */
    @PostMapping("/update")
    @ApiOperation(value = "助力活动", notes = "助力活动")
    public JsonResult update(@RequestBody MdmHelpActivityVM req) {
        log.info("更新助力活动，入参req : {}", JSON.toJSONString(req));
        return activityService.update(req);
    }

    /**
     * 开启关闭活动
     */
    @PostMapping("/updateStatus")
    @ApiOperation(value = "开启关闭活动", notes = "开启关闭活动")
    public JsonResult updateStatus(@RequestBody MdmHelpActivityVM req) {
        log.info("开启关闭活动，入参req : {}", JSON.toJSONString(req));
        return activityService.updateStatus(req);
    }


    /**
     * 删除助力活动
     */
    @PostMapping("/delete")
    @ApiOperation(value = "助力活动", notes = "助力活动")
    public JsonResult delete(@RequestBody MdmHelpActivityVM req) {
        log.info("删除助力活动，入参req : {}", JSON.toJSONString(req));
        return activityService.delete(req);
    }

    /**
     * 助力活动列表
     * @param
     * @return
     */
    @PostMapping("/findByPage")
    @Timed
    @ApiOperation(value = "助力活动列表", tags = {"助力活动列表"}, notes = "助力活动列表")
    public JsonResult findByPage(@RequestBody MdmHelpActivityPageVM wocv ){
        JsonResult result = new JsonResult();
        try{
            Page<MdmHelpActivity> page = activityService.findByPage(wocv);
            result.setData(page);
            result.setState(0);
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("查询失败");
        }
        return result;
    }



    /**
     * 授权模板消息点击
     */
    @PostMapping("/confrimTemplate")
    @ApiOperation(value = "授权模板消息点击", notes = "授权模板消息点击")
    public JsonResult confrimTemplate(@RequestBody MdmTemplateConfirm req) {
        log.info("开启关闭活动，入参req : {}", JSON.toJSONString(req));
        return mdmTemplateConfirmService.confrimTemplate(req);
    }

}
